package com.rtc.call.core

import android.content.Context
import android.util.Log
import io.agora.rtc2.Constants
import io.agora.rtc2.IRtcEngineEventHandler
import io.agora.rtc2.RtcEngine
import io.agora.rtc2.video.BeautyOptions
import io.agora.rtc2.video.VideoEncoderConfiguration

/**
 * Manages the RTC engine instance and configuration
 */
object RtcEngineManager {
    
    /**
     * Create and configure an Agora RTC Engine instance
     */
    fun createRtcEngine(context: Context, appId: String): RtcEngine {
        return RtcEngine.create(context, appId, object : IRtcEngineEventHandler() {
            // Handle basic RTC events
            override fun onJoinChannelSuccess(channel: String, uid: Int, elapsed: Int) {
                // Handle successful join channel event
                Log.d("RtcEngineManager","onJoinChannelSuccess")
            }
            
            override fun onUserJoined(uid: Int, elapsed: Int) {
                // Handle remote user join event
                Log.d("RtcEngineManager","onUserJoined")
            }
            
            override fun onUserOffline(uid: Int, reason: Int) {
                // Handle remote user leave event
                Log.d("RtcEngineManager","onUserOffline")
            }

            override fun onAudioVolumeIndication(
                speakers: Array<out AudioVolumeInfo>?,
                totalVolume: Int
            ) {
                super.onAudioVolumeIndication(speakers, totalVolume)
            }
            
            override fun onConnectionStateChanged(state: Int, reason: Int) {
                // Handle connection state change
                Log.d("RtcEngineManager","onConnectionStateChanged")
            }
        }).apply {
            // Configure default RTC settings
            setChannelProfile(Constants.CHANNEL_PROFILE_COMMUNICATION)
            disableVideo()
            enableAudio()
            setEnableSpeakerphone(true)
            setDefaultAudioRoutetoSpeakerphone(true)
            enableDualStreamMode(true)
        }
    }
    
    /**
     * Configure the RTC engine for audio call
     */
    fun configureForAudioCall(rtcEngine: RtcEngine?) {
        rtcEngine?.apply {
            enableLocalAudio(true)
            enableLocalVideo(false) // Disable video for audio calls
            muteAllRemoteVideoStreams(true)
        }
    }
    
    /**
     * Configure the RTC engine for video call
     */
    fun configureForVideoCall(rtcEngine: RtcEngine?) {
        rtcEngine?.apply {
            enableLocalAudio(true)
            enableLocalVideo(true)
            muteAllRemoteVideoStreams(false)
            setVideoEncoderConfiguration(
               VideoEncoderConfiguration(
                    VideoEncoderConfiguration.VD_640x360,
                    VideoEncoderConfiguration.FRAME_RATE.FRAME_RATE_FPS_15,
                    VideoEncoderConfiguration.STANDARD_BITRATE,
                    VideoEncoderConfiguration.ORIENTATION_MODE.ORIENTATION_MODE_ADAPTIVE
                )
            )
        }
    }
    
    /**
     * Configure the RTC engine for colorful call (specific to the application)
     */
    fun configureForColorfulCall(rtcEngine: RtcEngine?) {
        rtcEngine?.apply {
            // Configure specific settings for colorful call as shown in the diagram
            enableLocalVideo(false)
            enableLocalAudio(true)
            setBeautyEffectOptions(true,
                BeautyOptions(
                   BeautyOptions.LIGHTENING_CONTRAST_NORMAL,
                    0.5f,
                    0.5f, // Redness
                    0.5f, // Sharpness
                    0.5f  // Smoothness
                )
            )
        }
    }


} 